package com.foton.logistic.util;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.foton.logistic.base.BaseApplication;
import com.foton.logistic.model.order.LocalOrderEntity;
import com.foton.logistic.model.order.LocationEntity;
import com.foton.logistic.model.order.OrderEntity;
import com.foton.logistic.service.LocationService;

import java.util.List;

public class OrderUtil {

    /**
     * 获取定位订单
     * @param context
     * @param orderData
     * @return
     */
    public static LocalOrderEntity getLocalOrder(Context context,OrderEntity orderData){
        LocalOrderEntity localOrderData = null;
        try {
            localOrderData = SharedUtil.getLocalOrderEntity(context);
            if (localOrderData!=null){
                if (!localOrderData.order.id.equals(orderData.id)){
                    if (localOrderData.order.schedId!=null&&orderData.schedId!=null){
                        if (!localOrderData.order.schedId.equals(orderData.schedId)){
                            localOrderData = null;
                        }
                    }else {
                        localOrderData = null;
                    }
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return localOrderData;
    }

    /**
     * 获取待上传轨迹条数
     * @param context
     * @return
     */
    public static int getTrackWaitCount(Context context){
        int waitCnt = 0;
        try {
            LocalOrderEntity localOrderData = SharedUtil.getLocalOrderEntity(context);
            if (localOrderData!=null){
                List<LocationEntity> list = LocationService.getInstance(context).queryByOrderId(localOrderData.order.schedId);
                if (list!=null){
                    waitCnt =  list.size();
                }

//                List<LocationEntity> list2 = LocationService.getInstance(context).queryByOrderId(true);
//                if (list2!=null){
//                    waitCnt =  list2.size();
//                    if (waitCnt>0){
//                        LogUtil.e("waitCnt2= "+list2.size());
//                        LogUtil.e("waitCnt2 localId= "+list2.get(0).localId);
//                    }
//                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return waitCnt;
    }


//    public static void startLocation(Activity activity,View view,OrderEntity orderData,List<OrderEntity> schedOrderList){
//        if (schedOrderList!=null&&schedOrderList.size()>0){
//            OrderListResult orderListResult = new OrderListResult();
//            orderListResult.saveMergeList(schedOrderList);
//            SharedUtil.saveOrderList(activity,orderListResult);
//        }else {
//            SharedUtil.saveOrderList(activity,"");
//        }
//        SharedUtil.saveOutServiceRoadOrder(activity, orderData);
//        SharedUtil.saveOrderTimer(activity, System.currentTimeMillis());
//        BaseApplication.self().startOut();
//        OptionUtil.isGpsOpen(activity, view);
//
//    }

    public static void startLocation(Activity activity,View view,OrderEntity orderData,List<OrderEntity> schedOrderList){
        LocalOrderEntity localOrderEntity = new LocalOrderEntity();
        localOrderEntity.setOrder(orderData);
        localOrderEntity.setSchedOrderList(schedOrderList);

        SharedUtil.saveLocalOrder(activity,localOrderEntity);
        SharedUtil.saveOrderTimer(activity, System.currentTimeMillis());
        BaseApplication.self().startOut();
        OptionUtil.isGpsOpen(activity, view);

    }

}